package com.wuseguang.springcloud.dao.jdbc;

import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import com.wuseguang.springcloud.entity.Holiday;
import com.wuseguang.springcloud.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BanCardJdbc extends BaseTemplate {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public List<Map<String, Object>> getMapList(String sql, Object... args) {
        logger.info("sql={}", sql);
        return dao.query(sql, args, rs -> {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("bancardid", rs.getString("bancardid"));
                map.put("classroomid", rs.getString("classroomid"));
                map.put("volume", rs.getString("volume"));
                map.put("classroomname", rs.getString("classroomname"));
                map.put("remarks", rs.getString("remarks"));
                map.put("deviceType", rs.getString("deviceType"));
                map.put("eDviceType", rs.getString("eDeviceType"));
                map.put("classid", rs.getString("classid"));
                map.put("classname", rs.getString("classname"));
                map.put("deviceid", rs.getString("deviceid"));
                map.put("version", rs.getString("version"));
                map.put("heartbeattime", rs.getString("heartbeattime"));
                map.put("monday", rs.getString("monday"));
                map.put("tuesday", rs.getString("tuesday"));
                map.put("wednesday", rs.getString("wednesday"));
                map.put("thursday", rs.getString("thursday"));
                map.put("friday", rs.getString("friday"));
                map.put("saturday", rs.getString("saturday"));
                map.put("sunday", rs.getString("sunday"));
                map.put("screenshot", rs.getString("screenshot"));
                map.put("screentime", rs.getString("screentime"));
                map.put("state", rs.getString("state"));
                maps.add(map);
            }
            return maps;
        });
    }


    public List<Map<String, Object>> getMapList2(String sql, Object... args) {
        logger.info("sql={}", sql);
        return dao.query(sql, args, rs -> {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", rs.getString("id"));
                map.put("name", rs.getString("name"));
                map.put("holiday_st", rs.getString("holiday_st"));
                map.put("holiday_et", rs.getString("holiday_et"));
                String state = rs.getString("state");
                map.put("state", (state == null || "".equals(state)) ? "0" : state);
                map.put("opertime", rs.getString("opertime"));
                maps.add(map);
            }
            return maps;
        });
    }

    public Map<String, Object> queryMap1(String sql, Object... arg) {
        return dao.query(sql, arg, rs -> {
            Map<String, Object> map = new HashMap<>();
            //monday,tuesday,wednesday,thursday,friday,saturday,sunday
            while (rs.next()) {
                String monday = rs.getString("monday");
                String tuesday = rs.getString("tuesday");
                String wednesday = rs.getString("wednesday");
                String thursday = rs.getString("thursday");
                String friday = rs.getString("friday");
                String saturday = rs.getString("saturday");
                String sunday = rs.getString("sunday");
                if ((monday == null || "".equals(monday)) && (tuesday == null || "".equals(tuesday)) && (wednesday == null || "".equals(wednesday)) && (thursday == null || "".equals(thursday)) && (friday == null || "".equals(friday)) && (saturday == null || "".equals(saturday)) && (sunday == null || "".equals(sunday))) {
                    return map;
                }
                map.put("monday", monday);
                map.put("tuesday", tuesday);
                map.put("wednesday", wednesday);
                map.put("thursday", thursday);
                map.put("friday", friday);
                map.put("saturday", saturday);
                map.put("sunday", sunday);
            }
            return map;
        });
    }


    public int batchUpdateDepartMent(String[] ids, List<Holiday> holidayList) {
        int a = 1;
        try {
            String sql = "DELETE FROM holiday_deviceid WHERE device_id IN ( ";
            String sql1 = "INSERT INTO  holiday_deviceid(holiday_id,device_id,holiday_st,holiday_et,state,opertime)  SELECT ?,d.id,?,?,?,? FROM bancard d WHERE d.id IN ( ";
            for (int i = 0; i < ids.length; i++) {
                sql += ids[i] + ",";
                sql1 += ids[i] + ",";
            }
            // 去除最后一个空格
            sql = sql.substring(0, sql.length() - 1) + ")";
            sql1 = sql1.substring(0, sql1.length() - 1) + ")";
            int i = updateByParameter(sql);
            dao.batchUpdate(sql1, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    Holiday holiday = holidayList.get(i);
                    logger.info("setValues......");
                    ps.setString(1, holiday.getId() + "");
                    ps.setString(2, holiday.getHoliday_st());
                    ps.setString(3, holiday.getHoliday_et());
                    ps.setInt(4, holiday.getState());
                    ps.setString(5, DateUtil.time());
                }

                @Override
                public int getBatchSize() {
                    return holidayList.size();
                }
            });

        } catch (Exception e) {
            a = 0;
            e.printStackTrace();
            logger.info("批量添加异常,error={}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
        }
        return a;
    }
}
